#include"admin_system_user_manage.h"

//修改用户
void edit_userinfo_event_cb(lv_event_t *e)
{
    FindData *data = lv_event_get_user_data(e);
    char *origin = lv_textarea_get_text(data->find_goods_id_ta);
    char *account = lv_textarea_get_text(data->find_goods_name_ta);
    char *password = lv_textarea_get_text(data->find_goods_type_dp);
    char *identity = lv_textarea_get_text(data->tmp6);
    char *credits = lv_textarea_get_text(data->tmp5);
    char *discount = lv_textarea_get_text(data->tmp7);
    float dis = atof(discount);

    User *tmp = user_find_node_by_username(user_list,origin);
    if(tmp == NULL)
    {
        lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The account does not exist.", NULL, true);
        lv_obj_center(mbox);
        return;
    }
    //判断更改的用户名是否存在
    User *s = user_find_node_by_username(user_list,account);
    if(s != NULL)
    {
        lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "This username has already been taken.", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    if(strlen(account) == 0 && strlen(password) == 0 && strlen(identity) == 0 && strlen(credits) == 0 && strlen(discount) == 0)
    {
        return;
    }
    if(strlen(account) != 0)
    {
        if(strlen(account) > 18)
        {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "account length: 1 to 18", NULL, true);
            lv_obj_center(mbox);
            return;
        }
        strcpy(tmp->username,account);
    }
    if(strlen(password) != 0)
    {
        if(strlen(password) > 18)
        {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "password length: 1 to 18", NULL, true);
            lv_obj_center(mbox);
            return;
        }
        strcpy(tmp->password,password);
    }
    if(strlen(identity) != 0)
    {
        int id = atoi(identity);
        if(id > 3 || id < 0)
        {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "id range: 0 to 3", NULL, true);
            lv_obj_center(mbox);
            return;
        }
        tmp->identity = id;
    }
    if(strlen(credits) != 0)
    {
        int credit = atoi(credits);
        if(credit > 99999 || credit < 0)
        {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "credit range: 0 to 99999", NULL, true);
            lv_obj_center(mbox);
            return;
        }
        tmp->credits = credit;
    }
    if(strlen(discount) != 0)
    {
        float dis = atof(discount);
        if(dis > 1.0 || dis < 0.1)
        {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "Discount range: 0.1 to 1", NULL, true);
            lv_obj_center(mbox);
            return;
        }
        tmp->discount = dis;
    }
    user_file_save_data(user_list,"user.txt");
    char log_buf[256];
    snprintf(log_buf,sizeof(log_buf),"Modified user information");
    log_add(LOG_UPDATE_ITEM,cur_user.username,cur_user.identity,log_buf);

    lv_textarea_set_text(data->find_goods_id_ta,tmp->username);
    FindData *find_data =malloc(sizeof(FindData));
    if (find_data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }

    find_data->find_goods_id_ta = data->find_goods_id_ta;
    find_data->goods_table_cont = data->goods_table_cont;
    show_user_info(find_data);
}


//修改用户信息的展示
void show_user_info(FindData *data)
{    
    char *account = lv_textarea_get_text(data->find_goods_id_ta);
    User *tmp = user_find_node_by_username(user_list,account);
    //创建表格对象
    lv_obj_t *find_user_table =lv_table_create(data->goods_table_cont);
    lv_obj_set_size(find_user_table,800,130);
    lv_obj_set_style_border_width(find_user_table, 0, 0);
    lv_obj_set_style_pad_all(find_user_table, 0, 0);
    lv_obj_align(find_user_table,LV_ALIGN_TOP_MID,0,0);
    // 设置表格样式
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_border_width(&style, 1);                   // 边框宽度
    lv_style_set_border_color(&style, lv_palette_main(LV_PALETTE_BLUE)); // 边框颜色
    lv_style_set_bg_color(&style, lv_color_hex(0xf0f0f0));  // 背景色
    lv_style_set_pad_all(&style, 5);                        // 内边距
    //应用样式
    lv_obj_add_style(find_user_table, &style, 0);
    //设置表格的行列数
    lv_table_set_col_cnt(find_user_table,7);
    lv_table_set_row_cnt(find_user_table,2);
    //设置列宽
    lv_table_set_col_width(find_user_table, 0, 110);  // 第 0 列宽度 100px
    lv_table_set_col_width(find_user_table, 1, 110);  // 第 1 列宽度 100px
    lv_table_set_col_width(find_user_table, 2, 105);  // 第 2 列宽度 100px
    lv_table_set_col_width(find_user_table, 3, 100);  // 第 2 列宽度 100px
    lv_table_set_col_width(find_user_table, 4, 105);  // 第 2 列宽度 100px
    lv_table_set_col_width(find_user_table, 5, 110);  // 第 2 列宽度 100px
    lv_table_set_col_width(find_user_table, 6, 160);  // 第 2 列宽度 100px

    char buf_identity[20];
    char buf_credits[20];
    char buf_balance[20];
    char buf_Total_consumption[20];
    char buf_discount[20];
    snprintf(buf_identity,sizeof(buf_identity),"%d",tmp->identity);
    snprintf(buf_credits,sizeof(buf_credits),"%d",tmp->credits);
    snprintf(buf_balance,sizeof(buf_balance),"%.2f",tmp->balance);
    snprintf(buf_discount,sizeof(buf_discount),"%.2f",tmp->discount);
    snprintf(buf_Total_consumption,sizeof(buf_Total_consumption),"%.2f",tmp->Total_consumption);
    //表头设置
    lv_table_set_cell_value(find_user_table, 0, 0, "username");    
    lv_table_set_cell_value(find_user_table, 0, 1, "password");    
    lv_table_set_cell_value(find_user_table, 0, 2, "identity");
    lv_table_set_cell_value(find_user_table, 0, 3, "credits");
    lv_table_set_cell_value(find_user_table, 0, 4, "balance");
    lv_table_set_cell_value(find_user_table, 0, 5, "disc");
    lv_table_set_cell_value(find_user_table, 0, 6, "Total_consump");

    lv_table_set_cell_value(find_user_table, 1, 0, tmp->username);    
    lv_table_set_cell_value(find_user_table, 1, 1, tmp->password);    
    lv_table_set_cell_value(find_user_table, 1, 2, buf_identity);
    lv_table_set_cell_value(find_user_table, 1, 3, buf_credits);
    lv_table_set_cell_value(find_user_table, 1, 4, buf_balance);
    lv_table_set_cell_value(find_user_table, 1, 5, buf_discount);
    lv_table_set_cell_value(find_user_table, 1, 6, buf_Total_consumption);

    //用户名
    //  2.2   name
    lv_obj_t * edit_name_label = lv_label_create(data->goods_table_cont);
    lv_label_set_text(edit_name_label, "account:");
    lv_obj_align_to(edit_name_label,find_user_table, LV_ALIGN_OUT_BOTTOM_LEFT, 10, 20);

    lv_obj_t *edit_user_name = lv_textarea_create(data->goods_table_cont);
    lv_obj_align_to(edit_user_name,find_user_table,LV_ALIGN_OUT_BOTTOM_LEFT,75,5);
    lv_obj_set_size(edit_user_name,140,50);
    lv_obj_remove_style(edit_user_name, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(edit_user_name, "account");
    lv_textarea_set_max_length(edit_user_name, 8);
    lv_textarea_set_one_line(edit_user_name, true);
    lv_obj_add_event_cb(edit_user_name,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //  2.2   password
    lv_obj_t * edit_password_label = lv_label_create(data->goods_table_cont);
    lv_label_set_text(edit_password_label, "password:");
    lv_obj_align_to(edit_password_label,find_user_table, LV_ALIGN_OUT_BOTTOM_LEFT, 230, 20);

    lv_obj_t *edit_user_ps = lv_textarea_create(data->goods_table_cont);
    lv_obj_align_to(edit_user_ps,find_user_table,LV_ALIGN_OUT_BOTTOM_LEFT,305,5);
    lv_obj_set_size(edit_user_ps,140,50);
    lv_obj_remove_style(edit_user_ps, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(edit_user_ps, "password");
    lv_textarea_set_max_length(edit_user_ps, 8);
    lv_textarea_set_one_line(edit_user_ps, true);
    lv_obj_add_event_cb(edit_user_ps,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //  2.3   identity
    lv_obj_t * edit_identity_label = lv_label_create(data->goods_table_cont);
    lv_label_set_text(edit_identity_label, "identity:");
    lv_obj_align_to(edit_identity_label,find_user_table, LV_ALIGN_OUT_BOTTOM_LEFT, 10, 75);

    lv_obj_t *edit_user_id = lv_textarea_create(data->goods_table_cont);
    lv_obj_align_to(edit_user_id,find_user_table,LV_ALIGN_OUT_BOTTOM_LEFT,75,60);
    lv_obj_set_size(edit_user_id,140,50);
    lv_obj_remove_style(edit_user_id, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(edit_user_id, "identity");
    lv_textarea_set_accepted_chars(edit_user_id, "012");
    lv_textarea_set_max_length(edit_user_id, 1);
    lv_textarea_set_one_line(edit_user_id, true);
    lv_obj_add_event_cb(edit_user_id,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //2.4 credits
    lv_obj_t * edit_credits_label = lv_label_create(data->goods_table_cont);
    lv_label_set_text(edit_credits_label, "credits:");
    lv_obj_align_to(edit_credits_label,find_user_table, LV_ALIGN_OUT_BOTTOM_LEFT, 240, 75);

    lv_obj_t *edit_user_credits = lv_textarea_create(data->goods_table_cont);
    lv_obj_align_to(edit_user_credits,find_user_table,LV_ALIGN_OUT_BOTTOM_LEFT,305,60);
    lv_obj_set_size(edit_user_credits,140,50);
    lv_obj_remove_style(edit_user_credits, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(edit_user_credits, "credits");
    lv_textarea_set_accepted_chars(edit_user_credits, "0123456789");
    lv_textarea_set_max_length(edit_user_credits, 10);
    lv_textarea_set_one_line(edit_user_credits, true);
    lv_obj_add_event_cb(edit_user_credits,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //2.5 discount
    lv_obj_t * edit_discount_label = lv_label_create(data->goods_table_cont);
    lv_label_set_text(edit_discount_label, "discount:");
    lv_obj_align_to(edit_discount_label,find_user_table, LV_ALIGN_OUT_BOTTOM_LEFT, 10, 130);

    lv_obj_t *edit_user_discount = lv_textarea_create(data->goods_table_cont);
    lv_obj_align_to(edit_user_discount,find_user_table,LV_ALIGN_OUT_BOTTOM_LEFT,80,120);
    lv_obj_set_size(edit_user_discount,140,50);
    lv_obj_remove_style(edit_user_discount, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(edit_user_discount, "discount");
    lv_textarea_set_accepted_chars(edit_user_discount, "0123456789.");
    lv_textarea_set_max_length(edit_user_discount, 3);
    lv_textarea_set_one_line(edit_user_discount, true);
    lv_obj_add_event_cb(edit_user_discount,keyboard_event_cb,LV_EVENT_ALL,NULL);

    //2.5修改按钮
    lv_obj_t *edit_btn = lv_btn_create(data->goods_table_cont);
    lv_obj_set_size(edit_btn,80,50);
    lv_obj_align_to(edit_btn,find_user_table, LV_ALIGN_OUT_BOTTOM_LEFT, 460, 30);
    lv_obj_set_style_bg_color(edit_btn,lv_palette_main(LV_PALETTE_RED),0);
    
    lv_obj_t *edit_label = lv_label_create(edit_btn);
    lv_label_set_text(edit_label,"Edit");
    lv_obj_set_style_text_font(edit_label,&lv_font_montserrat_20,0);
    lv_obj_center(edit_label);

    FindData *edit_data =malloc(sizeof(FindData));
    if (edit_data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    edit_data->find_goods_id_ta = data->find_goods_id_ta;//原本的
    edit_data->goods_table_cont = data->goods_table_cont;//表格容器
    edit_data->find_goods_name_ta = edit_user_name;//用户名
    edit_data->find_goods_type_dp = edit_user_ps;//密码
    edit_data->tmp5 = edit_user_credits;//积分
    edit_data->tmp6 = edit_user_id;//身份
    edit_data->tmp7 = edit_user_discount;//身份

    lv_obj_add_event_cb(edit_btn,edit_userinfo_event_cb,LV_EVENT_CLICKED,edit_data);
}

//查找用户，并调用显示
void find_user_event_cb(lv_event_t *e)
{
    FindData *data = lv_event_get_user_data(e);
    char *account = lv_textarea_get_text(data->find_goods_id_ta);
    if(strlen(account) == 0)
    {
        lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "Please enter your username.", NULL, true);
        lv_obj_center(mbox);
        return;
    }
    User *tmp = user_find_node_by_username(user_list,account);
    if(tmp == NULL)
    {
        lv_obj_t * mbox = lv_msgbox_create(NULL, "Reminder", "The user does not exist.", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    show_user_info(data);

}

//修改用户信息的界面
void modify_user_info_ui(void)
{
    // 创建内容容器
    lv_obj_t * cont = lv_win_get_content(input_win);
    lv_obj_set_style_border_width(cont, 0, 0);
    lv_obj_set_style_pad_all(cont, 0, 0);

    //2.创建搜索框
    lv_obj_t * find_user_cont = lv_obj_create(cont);
    lv_obj_set_size(find_user_cont, 800,80);
    lv_obj_set_style_border_width(find_user_cont, 0, 0);
    lv_obj_set_style_pad_all(find_user_cont, 0, 0);
    lv_obj_remove_style(find_user_cont, NULL, LV_PART_SCROLLBAR);

    //  根据账户搜索用户
    lv_obj_t * find_user_label = lv_label_create(find_user_cont);
    lv_label_set_text(find_user_label, "Account:");
    lv_obj_align(find_user_label, LV_ALIGN_LEFT_MID, 250, 0);

    lv_obj_t *find_user_name = lv_textarea_create(find_user_cont);
    lv_obj_align(find_user_name,LV_ALIGN_LEFT_MID,325,0);
    lv_obj_set_size(find_user_name,150,50);
    lv_obj_remove_style(find_user_name, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_placeholder_text(find_user_name, "Account");
    lv_textarea_set_max_length(find_user_name, 8);
    lv_textarea_set_one_line(find_user_name, true);
    lv_obj_add_event_cb(find_user_name,keyboard_event_cb,LV_EVENT_ALL,NULL);


    //创建修改的容器
    lv_obj_t * user_info_cont = lv_obj_create(cont);
    lv_obj_set_size(user_info_cont, 800,330);
    lv_obj_align_to(user_info_cont,find_user_cont,LV_ALIGN_OUT_BOTTOM_MID,0,0);
    lv_obj_set_style_border_width(user_info_cont, 0, 0);
    lv_obj_set_style_pad_all(user_info_cont, 0, 0);
    lv_obj_remove_style(user_info_cont, NULL, LV_PART_SCROLLBAR);

    FindData *find_data =malloc(sizeof(FindData));
    if (find_data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    find_data->find_goods_id_ta = find_user_name;
    find_data->goods_table_cont = user_info_cont;
    //  查找按钮
    lv_obj_t *find_btn = lv_btn_create(find_user_cont);
    lv_obj_set_size(find_btn,80,45);
    lv_obj_align_to(find_btn,find_user_name, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(find_btn,find_user_event_cb,LV_EVENT_CLICKED,(void*)find_data);
    lv_obj_t * find_btn_label = lv_label_create(find_btn);
    lv_label_set_text(find_btn_label, "find");
    lv_obj_center(find_btn_label);

}


